home *** CD-ROM | disk | FTP | other *** search
/ Experimental BBS Explossion 3 / Experimental BBS Explossion III.iso / c / cp1.zip / MYLINE.CPP < prev    next >
C/C++ Source or Header  |  1993-06-14  |  3KB  |  90 lines

  1. ===========================================================================
  2.  BBS: The Abacus * HST/DS * Potterville MI
  3. Date: 06-13-93 (12:23)             Number: 141
  4. From: DAVID NUGENT                 Refer#: NONE
  5.   To: ALL                           Recvd: NO  
  6. Subj: [10 of 12] myLine.cpp          Conf: (37) C++ Langua
  7. ---------------------------------------------------------------------------
  8. // myLine.cpp
  9. //
  10. // 13 Jun 93   Init array[0] = NUL in case it is reference before use
  11. //             memcpy() adjusted to also copy terminating NUL from is.get()
  12. //             when extending buffer
  13. //
  14.  
  15. # include <iostream.h>
  16. # include "myLine.h"
  17. # if defined(_MSC_VER)
  18. #  include <memory.h>
  19. # else
  20. #  include <stdlib.h>
  21. # endif
  22.  
  23.     // Class implementation
  24.  
  25. myLine::myLine (short buflen)
  26.     : len(buflen), mybuf(new char[len]), xalloc(1)
  27. {
  28.     mybuf[0] = 0;
  29. }
  30.  
  31. myLine::myLine (char * usebuf, short buflen)
  32.     : len(buflen), mybuf(usebuf), xalloc(0)
  33. {
  34.     mybuf[0] = 0;
  35. }
  36.  
  37. myLine::~myLine (void)
  38. {
  39.     if (xalloc)
  40.         delete [] mybuf;
  41. }
  42.  
  43.  
  44. istream &
  45. operator>> (istream & is, myLine & l)
  46. {
  47. # if AUTO_GROW
  48.     if (!l.xalloc)              // It's not my buf, so it can't be grown
  49.     {
  50. # endif
  51.         is.get (l.mybuf, l.len);
  52.         if (is.peek() == '\n')
  53.             is.get();           // Remove newline from stream
  54. # if AUTO_GROW
  55.     }
  56.     else
  57.     {
  58.         int idx = 0;
  59.         l.mybuf[0] = 0;     // Terminate in case is.good() isn't
  60.         for (int eol = 0; !eol && is.good(); )
  61.         {
  62.             int toget = l.len - idx;
  63.             is.get (l.mybuf + idx, toget);
  64.             int chrs = is.gcount();
  65.             if (is.peek() == '\n')
  66.             {
  67.                 ++eol;       // Must be eol or eof
  68.                 is.get();    // Clear newline
  69.             }
  70.             else if (chrs)
  71.             {                // Extend buffer
  72.                 idx += chrs; // Add to index
  73.                 l.len = short(l.len + ALLOC_LEN);
  74.                 char * tmp = new char[l.len];
  75.                 memcpy (tmp, l.mybuf, idx + 1);
  76.                 delete [] l.mybuf;
  77.                 l.mybuf = tmp;
  78.             }
  79.         }
  80.     }
  81. # endif
  82.     return is;
  83. }
  84.  
  85. --- MaltEd 1.0.b5
  86.  * Origin: Unique Computing Pty Ltd (3:632/348)
  87. SEEN-BY: 1/211 11/2 4 13/13 101/1 109/25 114/5 123/19 124/1 153/752 154/40
  88. SEEN-BY: 154/77 157/110 159/100 125 140 180 270 430 575 950 203/23 209/209
  89. SEEN-BY: 261/1023 280/1 390/1 396/1 5 15 2430/1 2440/5 3603/20
  90.